//
//  YYEEmoticonTools.swift
//  weibo14
//
//  Created by mac on 16/3/13.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

// 一页的表情数量
private let YYEEmoticonsPageCount = 20


class YYEEmoticonTools: NSObject {

    static let shareEmoticonTools = YYEEmoticonTools()
    
    private override init() {}
    
    
    lazy var emoticonsBundle: NSBundle = {
    
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        return NSBundle(path: path!)!
    }()
    
    
    private func emoticonsWithFolderName(folderName: String) -> [YYEEmoticon] {
    
        let path = self.emoticonsBundle.pathForResource("\(folderName)/info.plist", ofType: nil)!
    
        let array = NSArray(contentsOfFile: path)!
        
        var result = [YYEEmoticon]()
        
        for vale in array {
            let modle = YYEEmoticon(dict: vale as! [String : AnyObject])
            
            modle.folderName = folderName

            result.append(modle)
        }

        return result
    }

    // 组织三维数组数据
    lazy var allEmoticons: [[[YYEEmoticon]]] = {
        return [
            [self.recentEmoticons],
            self.typeEmoticonPages(self.defaultEmoticons),
            self.typeEmoticonPages(self.emojiEmoticons),
            self.typeEmoticonPages(self.lxhEmoticons)
            
        ]
    }()

    // 组织二维数组的方法
    private func typeEmoticonPages(emoticons: [YYEEmoticon]) -> [[YYEEmoticon]] {
        
        var emoticonArray = [[YYEEmoticon]]()
        var tmp: Int = 0;
        
        for i in 0..<emoticons.count/YYEEmoticonsPageCount {
            // 108   循环5变 0 1 2 3 4
            tmp = emoticons.count/YYEEmoticonsPageCount
            var tempArray:[YYEEmoticon] =  [YYEEmoticon]()
            for j in 0..<20 { //每一页的数据
 
                // emoticonArray.append(emoticons[i * 20 + j])
                // 组成一个20个 一维数据
                tempArray.append(emoticons[i * 20 + j])
            }
            emoticonArray.append(tempArray)
        }
  
        if 0 < emoticons.count%YYEEmoticonsPageCount {
            
            var tempArray1:[YYEEmoticon] =  [YYEEmoticon]()
            for j in 0..<emoticons.count%YYEEmoticonsPageCount  { //每一页的数据
                
                // emoticonArray.append(emoticons[i * 20 + j])
                // 组成一个8个的 一维数据
                tempArray1.append(emoticons[20 * tmp + j])
            }
            emoticonArray.append(tempArray1)
        }
        return emoticonArray
    }
    
    /// 最近表情的保存路径
    private lazy var recentEmoticonPath: String = {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("recente.archive")
        return path
    }()
    
    // MARK: - 供外界调用的方法
    
    func saveRecentEmoticon(emoticon: YYEEmoticon) {
        
        //         filter 过滤出自己需要的数据
        let result = recentEmoticons.filter { (value) -> Bool in
            if value.type == "0" {
                // 判断png是否相同
                if value.png == emoticon.png {
                    // 两个表情是同一个表情
                    return true
                }
            }else{
                // 判断code是否同
                if value.code == emoticon.code {
                    return true
                }
            }
            return false
        }
        
        
        for value in result {
            let index = recentEmoticons.indexOf(value)!
            recentEmoticons.removeAtIndex(index)
        }
        
        
        // 1. 把表情加到最近表情的数组里面
        recentEmoticons.insert(emoticon, atIndex: 0)
        
        // 如果个数超过20,移除掉
        while recentEmoticons.count > 20 {
            recentEmoticons.removeLast()
        }
        
        // 2. 把数组归档
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: recentEmoticonPath)
        
        // 更新表情数据里面的最近表情的数据
        allEmoticons[0][0] = recentEmoticons
    }
    
    
    /// 通过表情字符串,去查找其对应的表情模型
    ///
    /// - parameter chs: 表情字符串
    
    func emoticonWithChs(chs: String) -> YYEEmoticon? {
        
        
        // 1. 从默认里面取
        let defaultResults = defaultEmoticons.filter { (emoticon) -> Bool in
            if emoticon.chs == chs {
                return true
            }
            return false
        }
        
        if defaultResults.count > 0 {
            return defaultResults.first
        }
        
        // 2. 从浪小花里面取
        let lxhResults = lxhEmoticons.filter { (emoticon) -> Bool in
            if emoticon.chs == chs {
                return true
            }
            return false
        }
        
        if lxhResults.count > 0 {
            return lxhResults.first
        }
        
        return nil
    }
    

    
    // 最近表情
    lazy var recentEmoticons: [YYEEmoticon] = {
        return [YYEEmoticon]()
    }()
    
    // Emoji表情
    lazy var emojiEmoticons: [YYEEmoticon] = {
        return self.emoticonsWithFolderName("emoji")
    }()
    
    /// 浪小花表情
    lazy var lxhEmoticons: [YYEEmoticon] = {
        return self.emoticonsWithFolderName("lxh")
    }()

    /// 读取默认表情
    lazy var defaultEmoticons: [YYEEmoticon] = {
        return self.emoticonsWithFolderName("default")
    }()
    
}



